#!/bin/sh
main()
{
. $DOCUMENT_ROOT/apps/home/header.tpl.sh
header_html

eval `cat $DOCUMENT_ROOT/apps/vlan/vlan.conf | sed '/^#/d' | grep =`
port_list=`echo "$Ports" | sed 's/,/\n/g'`
port_count=$(expr $(echo "$port_list" | wc -l) - 1)

cat <<'EOF'
<script>
$(function(){
  $('#post_vlan_setting').on('submit', function(e){
    e.preventDefault();
    var data = "app=vlan&"+$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'post_vlan_setting');
  });
});
</script>
EOF
cat <<EOF
		<div class="pure-g">
			<div class="l-box-lrg pure-u-1 pure-u-md-1-2">
				<h3>vlan设置</h3>
EOF

cat <<EOF
				<form id="post_vlan_setting">
					<table class="pure-table pure-table-bordered">
						<thead>
							<tr>
								<td>#</td>
EOF
for port_num in $port_list
do
# [ -z "$vlan_num" ] && vlan_num=0
cat <<EOF
<td>port${port_num}</td>
EOF

# vlan_num=`expr $vlan_num + 1`
done
cat <<EOF
							</tr>
						</thead>
EOF
for vlan_num in `seq 0 $port_count`
do
# [ -z "$vlan_num" ] && vlan_num=0
cat <<EOF
<tr>
	<td>vlan${vlan_num}</td>
EOF
	for port_num_next in $port_list
	do
	cat <<EOF
	<td><input type="radio" name="vlan_for_port${port_num_next}" id="port_for_vlan${vlan_num}" value="${vlan_num}" `[ "$($DOCUMENT_ROOT/apps/vlan/vlan.sbin queryport ${port_num_next})" = "${vlan_num}" ] && echo checked`></td>
EOF
	done
cat <<EOF
</tr>
EOF

# vlan_num=`expr $vlan_num + 1`
done

cat <<EOF
					</table>
					<input type="hidden" name="action" value="post_vlan_setting">
					<button type="submit" class="pure-button">保存</button>
				</form>
EOF

cat <<EOF
			</div>
			<div class="l-box-lrg pure-u-1 pure-u-md-1-2">
				<h3>说明</h3>
				
			</div>
		</div>
EOF
footer_html keep_bottom
}
post_vlan_setting()
{
$DOCUMENT_ROOT/apps/vlan/vlan.sbin del_all_vlan
$DOCUMENT_ROOT/apps/vlan/vlan.sbin add_vlan "$(env | grep "FORM_vlan_for_port" | sed 's/FORM_vlan_for_port//g' | awk -F "=" '{print $2"="$1}' | tr '\n' ' ')"
(echo "_LANG_Save_success" | main.sbin output_json 0) || exit 0
}

lang=`main.sbin get_client_lang`
eval `cat $DOCUMENT_ROOT/apps/$FORM_app/i18n/$lang/i18n.conf`
if
[ $is_main_page = 1 ]
then
main
elif [ -n "$FORM_action" ]
then
$FORM_action
fi
